package com.lanou;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description: Hello World!
 * author: 何雨轩
 * Create at: 18/7/6 下午2:35
 */
public class taotao {

    public static void main(String[] args) throws IOException {

        //爬虫爬取分类页面 获取分类规格
        String url = "https://list.jd.com/list.html?cat=737,794,870"; // 京东空调类页面
        Document document = Jsoup.connect(url).get();

        // 爬取名字为 #J_selector id选择器
        Elements elements = document.select("#J_selector");

        // 获取值参数
        // Elements elements1 = document.select("#J_selector");
        Elements elements1 = document.select(".J_valueList");

        int j = 0;
        // String n="";


        for (int i = 3; i < (elements1.size() + 2); i++) {

            for (Element element : elements) {
                // 获取类别名
                // #J_selector > div:nth-child(3) > div > div.sl-key > span
                String group = element.select("div:nth-child(" + i + ") > div > div.sl-key > span").text();

                List<String> list = new ArrayList<>();

                for (; j < elements1.size(); j++) {

                    // #J_selector > div:nth-child(5) > div > div.sl-value > div.sl-v-list > ul > li:nth-child(2) > a
                    String params = elements.get(j).select("div:nth-child(" + i + ") > div > div > div > ul > li > a").text();
//                    System.out.println(group + params);
//                    System.out.println("--------------------------------------------");

                    // 处理字符串
                    String k = "[' ']+"; //空格变成","
                    Pattern p = Pattern.compile(k);
                    Matcher m = p.matcher(params);
                    String s = m.replaceAll("\",\"".trim());
                    String t = "[\"" + s + "\"]"; // 前后加[""]
                    String r = group.substring(0, group.length() - 1);// 去掉group后面的:
                    String n = "{\"group\":\"" + r + "\",\"params\":" + t + "}";
                    list.add(n);
                    // System.out.println(n);

                    break;
                }
                System.out.print(list);
            }
        }
    }
}
